﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using CommonLibrary.Models;
using GeneticSharpDemo;
using Sunny.UI;

namespace AutoShuntingSchedule.UI.Forms
{
    public partial class ScheduleGraphForm : UIPage
    {
        public ToolStripMgr ToolStripMgr { get; } = new ToolStripMgr();

        public ScheduleGraphForm()
        {
            InitializeComponent();
            this.uiPanel1.AutoScroll = true;
            this.mainUserControl1.AutoCalClick += MainUserControl1OnAutoCalClick;
        }

        private void MainUserControl1OnAutoCalClick(object sender, EventArgs e)
        {
            AutoCal();
        }

        private void mainUserControl1_Load(object sender, EventArgs e)
        {
            if (DesignMode)
            {
                return;
            }

            SetupToolStripUi();
        }

        private void SetupToolStripUi()
        {
            // 主菜单
            MenuStrip menuMain = SetupMainMenu();
            //menuMain.Visible = false;
            this.MainMenuStrip = menuMain;

            // 工具条
            List<ToolStrip> toolStripList = SetupToolbar();

            // Top Panel
            ToolStripPanel topPanel = ToolStripMgr.GetToolStripPanle(DockStyle.Top, true);
            this.Controls.Add(topPanel);

            // 将主菜单和工具条添加到 Top Panel
            for (int i = toolStripList.Count - 1; i >= 0; --i)
            {
                topPanel.Join(toolStripList[i]);
            }

            topPanel.Join(menuMain);
        }

        /// <summary>
        /// 设置工具条
        /// </summary>
        private List<ToolStrip> SetupToolbar()
        {
            List<ToolStrip> toolStripList = new List<ToolStrip>();

            // 文件
            ToolStrip fileToolStrip = SetupToolbar_File();
            toolStripList.Add(fileToolStrip);

            // 编辑
            ToolStrip editToolStrip = SetupToolbar_Edit();
            toolStripList.Add(editToolStrip);

            // 修改
            ToolStrip modifyToolStrip = SetupToolbar_Modify();
            toolStripList.Add(modifyToolStrip);

            return toolStripList;
        }

        /// <summary>
        /// 工具条: 文件
        /// </summary>
        private ToolStrip SetupToolbar_File()
        {
            ToolStrip fileToolStrip = ToolStripMgr.GetToolStrip("File", true);

            // 打开
            ToolStripButton open = ToolStripMgr.NewToolStripButton("file_open");
            open.Text = "";
            fileToolStrip.Items.Add(open);

            // 保存
            ToolStripButton save = ToolStripMgr.NewToolStripButton("file_save");
            save.Text = "";
            fileToolStrip.Items.Add(save);

            // 另存为
            ToolStripButton saveas = ToolStripMgr.NewToolStripButton("file_saveas");
            saveas.Text = "";
            fileToolStrip.Items.Add(saveas);

            return fileToolStrip;
        }

        private ToolStrip SetupToolbar_Modify()
        {
            ToolStrip modifyToolstrip = ToolStripMgr.GetToolStrip("Modify", true);

            // 删除
            ToolStripButton erase = ToolStripMgr.NewToolStripButton("modify_erase");
            modifyToolstrip.Items.Add(erase);

            // 移动
            ToolStripButton move = ToolStripMgr.NewToolStripButton("modify_move");
            modifyToolstrip.Items.Add(move);

            return modifyToolstrip;
        }

        private ToolStripButton _undoToolstripItem = null;
        private ToolStripButton _redoToolstripItem = null;
        private ToolStrip SetupToolbar_Edit()
        {
            ToolStrip editToolstrip = ToolStripMgr.GetToolStrip("Edit");

            // 撤销
            ToolStripButton undo = ToolStripMgr.NewToolStripButton("edit_undo");
            editToolstrip.Items.Add(undo);
            _undoToolstripItem = undo;

            // 重做
            ToolStripButton redo = ToolStripMgr.NewToolStripButton("edit_redo");
            editToolstrip.Items.Add(redo);
            _redoToolstripItem = redo;

            return editToolstrip;
        }

        private MenuStrip SetupMainMenu()
        {
            MenuStrip mainMenu = ToolStripMgr.GetMenuStrip("Main", true);
            SetupMainMenu_File(mainMenu);

            // 菜单: 编辑
            ToolStripMenuItem menuEdit = SetupMainMenu_Edit();
            mainMenu.Items.Add(menuEdit);

            // 菜单: 修改
            ToolStripMenuItem menuModify = SetupMainMenu_Modify();
            mainMenu.Items.Add(menuModify);

            return mainMenu;
        }

        private void SetupMainMenu_File(MenuStrip menuMain)
        {
            ToolStripMenuItem menuFile = new ToolStripMenuItem();
            menuFile.Text = "文件";
            menuMain.Items.Add(menuFile);

            // 打开
            ToolStripMenuItem open = ToolStripMgr.NewMenuItem(
                "file_open",
                "打开...",
                global::AutoShuntingSchedule.UI.Properties.Resources.file_open.ToBitmap(),
                this.OnFileOpen);
            menuFile.DropDownItems.Add(open);

            // 保存
            ToolStripMenuItem save = ToolStripMgr.NewMenuItem(
                "file_save",
                "保存",
                global::AutoShuntingSchedule.UI.Properties.Resources.file_save.ToBitmap(),
                this.OnFileSave);
            menuFile.DropDownItems.Add(save);

            // 另存为
            ToolStripMenuItem saveas = ToolStripMgr.NewMenuItem(
                "file_saveas",
                "另存为...",
                global::AutoShuntingSchedule.UI.Properties.Resources.file_saveas.ToBitmap(),
                this.OnFileSaveAs);
            menuFile.DropDownItems.Add(saveas);
        }

        private void OnFileSaveAs(object sender, EventArgs e)
        {
        }

        private void OnFileSave(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private void OnFileOpen(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private ToolStripMenuItem SetupMainMenu_Modify()
        {
            ToolStripMenuItem menuModify = new ToolStripMenuItem();
            menuModify.Text = "修改";

            // 删除
            ToolStripMenuItem erase = ToolStripMgr.NewMenuItem(
                "modify_erase",
                "删除",
                global::AutoShuntingSchedule.UI.Properties.Resources.modify_erase.ToBitmap(),
                this.OnModifyErase);
            menuModify.DropDownItems.Add(erase);

            // 移动
            ToolStripMenuItem move = ToolStripMgr.NewMenuItem(
                "modify_move",
                "移动",
                global::AutoShuntingSchedule.UI.Properties.Resources.modify_move.ToBitmap(),
                this.OnModifyMove);
            menuModify.DropDownItems.Add(move);

            return menuModify;
        }

        private void OnModifyMove(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private void OnModifyErase(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private ToolStripMenuItem SetupMainMenu_Edit()
        {
            ToolStripMenuItem menuEdit = new ToolStripMenuItem();
            menuEdit.Text = "编辑";

            // 撤销
            ToolStripMenuItem undo = ToolStripMgr.NewMenuItem(
                "edit_undo",
                "撤销",
                global::AutoShuntingSchedule.UI.Properties.Resources.edit_undo.ToBitmap(),
                this.OnEditUndo);
            undo.ShortcutKeys = Keys.Control | Keys.Z;
            menuEdit.DropDownItems.Add(undo);

            // 重做
            ToolStripMenuItem redo = ToolStripMgr.NewMenuItem(
                "edit_redo",
                "重做",
                global::AutoShuntingSchedule.UI.Properties.Resources.edit_redo.ToBitmap(),
                this.OnEditRedo);
            redo.ShortcutKeys = Keys.Control | Keys.Y;
            menuEdit.DropDownItems.Add(redo);

            return menuEdit;
        }

        private void OnEditRedo(object sender, EventArgs e)
        {
        }

        private void OnEditUndo(object sender, EventArgs e)
        {
        }

        private async void AutoCal()
        {
            MainFormCommonLib.Instance.SetLocationToForm(ClientRectangle);
            MainFormCommonLib.Instance.ActiveProgressIndicator(true);
            //主线程，界面线程
            await Task.Run(() =>
            {
                var schedules = GeneticSharpAlgorithmDemo.Start(null);//算法耗时
                this.Invoke(GraphShowScheduleList(), schedules);
            });
        }

        private Action<List<Schedule>> GraphShowScheduleList()
        {
            return x =>
            {
                this.mainUserControl1.InitData(x);
                this.mainUserControl1.Invalidate();
                MainFormCommonLib.Instance.ActiveProgressIndicator(false);
                this.Invalidate();
            };
        }
    }
}
